package czopyk.tests;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.NullOutputStream;

import pl.czopyk.view.QuickDrawer;

import com.espertech.esper.collection.Pair;

public class PerfTest1 {

	public static void main(String[] args) throws Exception {
		// testMultiple(1);
		test();
	}

	static String modelBase = "src/main/resources/models/perf/m2_{0}.xml";
	static String controllerBase = "src/main/resources/controllers/perf{0}/controller{1,number,#}.xml";

	static String[] modelParams = new String[] { "10u", "10u_ext1", "10u_ext2" };
	static Integer[] controllerParams = new Integer[] { 400, 1600, 3200, 6400 };

	static Map<Pair<String, Integer>, Integer> requestsCounts = new HashMap<Pair<String, Integer>, Integer>();

	static {
		for (int m = 0; m < modelParams.length; ++m) {
			for (Integer controller : controllerParams) {
				requestsCounts.put(
						new Pair<String, Integer>(modelParams[m], controller), controller
								* (m == 0 ? 1 : (m == 1 ? 6 : 18)));
			}
		}
	}

	public static void test() throws Exception {
		QuickDrawer.off = false;

		for (String eraClass : new String[] { csimClass, propietaryClass }) {
			test(0, 10000, 10000, eraClass);
			test(0, 30000, 30000, eraClass);
			test(0, 90000, 90000, eraClass);
		}

		for (String eraClass : new String[] { csimClass, propietaryClass }) {
			test(1, 1700, 10000, eraClass);
			test(1, 5100, 30000, eraClass);
			test(1, 15300, 90000, eraClass);
		}

		for (String eraClass : new String[] { csimClass, propietaryClass }) {
			test(2, 560, 10000, eraClass);
			test(2, 1670, 30000, eraClass);
			test(2, 5000, 90000, eraClass);
		}

		QuickDrawer.off = false;
		QuickDrawer.draw();
	}

	public static void test(Integer model, Integer controller, Integer requestsCount, String eraClass) throws Exception {
		double result = testMultiple(1, getModel(model),
				getControllerStrict(controller), eraClass);
		System.err.println(result);
		QuickDrawer.off = false;
		QuickDrawer.addXY(requestsCount, result,
				"Konf. " + (model == 0 ? "A" : (model == 1 ? "B" : "C")) + "; "
						+ (eraClass.equals(csimClass) ? "CSIM" : "Implementacja autorska"), "");
	}

	static String csimClass = "pl.czopyk.tests.Test1";
	static String propietaryClass = "czopyk.tests.Test1";

	// static Integer[] controllerParams = new Integer[] {400, 800};

	static String getModel(int param) {
		return MessageFormat.format(modelBase, new Object[] { modelParams[param] });
	}

	static String getControllerStrict(int param) {
		Object[] params = new Object[] { "", param };
		return MessageFormat.format(controllerBase, params);
	}

	static String getController(int param) {
		Object[] params = new Object[] { "", controllerParams[param] };
		return MessageFormat.format(controllerBase, params);
	}

	public static void testMultiple(int howMany) throws Exception {
		double[][] results = new double[modelParams.length][controllerParams.length];
		for (int i = 0; i < modelParams.length; ++i) {
			for (int j = 0; j < controllerParams.length; ++j) {
				for (String eraClass : new String[] { csimClass, propietaryClass }) {
					String model = getModel(i);
					String controller = getController(j);
					results[i][j] = testMultiple(howMany, model, controller, eraClass);
					System.err.println(results[i][j]);
					QuickDrawer.off = false;
					QuickDrawer.addXY(requestsCounts.get(new Pair<String, Integer>(
							modelParams[i], controllerParams[j])), results[i][j], eraClass
							.equals(csimClass) ? "CSIM" : "Implementacja autorska", "");
				}
			}
		}
		QuickDrawer.off = false;
		QuickDrawer.draw();
	}

	public static double testMultiple(int howMany, String model,
			String controller, String eraClass) throws Exception {
		int i = 0;
		double sum = 0.0;

		String command_new = "\"C:/Program Files/Java/jre1.6.0_20/bin/java.exe\" -Xms128M -Xmx180M -Dfile.encoding=UTF-8 -classpath \"C:/java/workspace/lastAttempt/target/classes;C:/Documents and Settings/czopson_/.m2/repository/jfree/jfreechart/1.0.13/jfreechart-1.0.13.jar;C:/Documents and Settings/czopson_/.m2/repository/jfree/jcommon/1.0.15/jcommon-1.0.15.jar;C:/Documents and Settings/czopson_/.m2/repository/log4j/log4j/1.2.14/log4j-1.2.14.jar;C:/Documents and Settings/czopson_/.m2/repository/sax/sax/2.0.1/sax-2.0.1.jar;C:/Documents and Settings/czopson_/.m2/repository/colt/colt/1.2.0/colt-1.2.0.jar;C:/Documents and Settings/czopson_/.m2/repository/concurrent/concurrent/1.3.4/concurrent-1.3.4.jar;C:/Documents and Settings/czopson_/.m2/repository/commons-lang/commons-lang/2.5/commons-lang-2.5.jar;C:/Documents and Settings/czopson_/.m2/repository/com/espertech/esper/4.0.0/esper-4.0.0.jar;C:/Documents and Settings/czopson_/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar;C:/Documents and Settings/czopson_/.m2/repository/org/antlr/antlr-runtime/3.2/antlr-runtime-3.2.jar;C:/Documents and Settings/czopson_/.m2/repository/org/antlr/stringtemplate/3.2/stringtemplate-3.2.jar;C:/Documents and Settings/czopson_/.m2/repository/antlr/antlr/2.7.7/antlr-2.7.7.jar;C:/Documents and Settings/czopson_/.m2/repository/cglib/cglib-nodep/2.2/cglib-nodep-2.2.jar;C:/Documents and Settings/czopson_/.m2/repository/com/google/guava/guava/r07/guava-r07.jar;C:/java/workspace/Drawer/target/classes;C:/java/workspace/MathExtension/target/classes\" czopyk.tests.Test1";
		String command_old = "\"C:/Program Files/Java/jre1.6.0_20/bin/java.exe\" -Xms128M -Xmx180M -Dfile.encoding=UTF-8 -classpath \"C:/Documents and Settings/czopson_/Pulpit/MGR/bitbucket/mgr/target/classes;C:/java/jars/CSIM_For_Java/csimForJava.jar;C:/Documents and Settings/czopson_/.m2/repository/jfree/jfreechart/1.0.13/jfreechart-1.0.13.jar;C:/Documents and Settings/czopson_/.m2/repository/jfree/jcommon/1.0.15/jcommon-1.0.15.jar;C:/Documents and Settings/czopson_/.m2/repository/log4j/log4j/1.2.14/log4j-1.2.14.jar;C:/Documents and Settings/czopson_/.m2/repository/sax/sax/2.0.1/sax-2.0.1.jar;C:/Documents and Settings/czopson_/.m2/repository/colt/colt/1.2.0/colt-1.2.0.jar;C:/Documents and Settings/czopson_/.m2/repository/concurrent/concurrent/1.3.4/concurrent-1.3.4.jar;C:/Documents and Settings/czopson_/.m2/repository/commons-lang/commons-lang/2.5/commons-lang-2.5.jar;C:/Documents and Settings/czopson_/.m2/repository/com/espertech/esper/4.0.0/esper-4.0.0.jar;C:/Documents and Settings/czopson_/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar;C:/Documents and Settings/czopson_/.m2/repository/org/antlr/antlr-runtime/3.2/antlr-runtime-3.2.jar;C:/Documents and Settings/czopson_/.m2/repository/org/antlr/stringtemplate/3.2/stringtemplate-3.2.jar;C:/Documents and Settings/czopson_/.m2/repository/antlr/antlr/2.7.7/antlr-2.7.7.jar;C:/Documents and Settings/czopson_/.m2/repository/cglib/cglib-nodep/2.2/cglib-nodep-2.2.jar;C:/Documents and Settings/czopson_/.m2/repository/com/google/guava/guava/r07/guava-r07.jar;C:/java/workspace/Drawer/target/classes;C:/java/workspace/MathExtension/target/classes\" pl.czopyk.tests.Test1";

		String command = (eraClass.equals(csimClass) ? command_old : command_new)
				+ " " + model + " " + controller;
		while (i++ < howMany) {
			double t = System.currentTimeMillis();
			System.err.println("executing command: " + command);
			Process p = Runtime.getRuntime().exec(command);
			// IOUtils.copy(p.getInputStream(), System.out);
			IOUtils.copy(p.getInputStream(), new NullOutputStream());
			IOUtils.copy(p.getErrorStream(), new NullOutputStream());
			// IOUtils.copy(p.getErrorStream(), System.err);
			p.waitFor();
			t = System.currentTimeMillis() - t;
			sum += t;
		}
		return ((sum / 1000.0) / howMany);
	}
}